package com.cy.pj.brand.controller;

import com.cy.pj.brand.pojo.Brand;
import com.cy.pj.brand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/brand/")
public class BrandController {

    @Autowired
    private BrandService brandService;

    //更新
    @RequestMapping("doUpdateBrand")
    public String doUpdateBrand(Brand brand, Model model) {
        int rows = brandService.updateBrand(brand);
        List<Brand> list = brandService.findBrands(null);//执行查询
        model.addAttribute("list", list);
        return "brand";
    }

    //更新带数据
    @RequestMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id,Model model){
        Brand brand = brandService.findById(id);
        model.addAttribute("brand", brand);
        return "brand-update";
    }

    //添加
    @RequestMapping("doSaveBrand")
    public String doSaveBrand(Brand brand, Model model) {
        int rows = brandService.saveBrand(brand);
        List<Brand> list = brandService.findBrands(null);//执行查询
        model.addAttribute("list", list);
        return "brand";
    }

    //添加页面
    @RequestMapping("doAddUI")
    public String doAddUI() {
        return "brand-add";
    }

    //删除
    @RequestMapping("doDeleteById/{id}")
    public String doDeleteById(@PathVariable Integer id, Model model) {
        System.out.println("xxxx");
        brandService.deleteById(id);
        List<Brand> list = brandService.findBrands(null);//执行查询
        model.addAttribute("list", list);
        return "brand";
    }

    @RequestMapping({"doFindBrands/{name}", "/doFindBrands"})
    public String doFindBrands(@PathVariable(required = false) String name, Model model) {
        List<Brand> list = brandService.findBrands(name);
        model.addAttribute("list", list);
        return "brand";
    }
}
